// pages/order/expressInfo/expressInfo.js
import {
  getExpress
} from '../../../api/order'
import { expressCompanyList}  from '../../../const/company.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    oid: String,
    base: Object,
    goodImg: String,
  },

  /**
   * 组件的初始数据
   */
  data: {
    companyName: '',
    info: null,
    list: [],
    nu: '',
    companyName:'',
   
  },
  lifetimes: {
    attached() {
      console.log('initExpress')
      this.initExpress()

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    copy(){
      let nu = this.data.nu
      // console.log(this.data)
      wx.setClipboardData({
        data: nu,
        success:(res) =>{
          wx.showToast({
            title: '快递单号已复制',
            icon: 'none'
          })
        }
      })
    },
    close() {
      this.triggerEvent('close')
    },
    initExpress() {
      console.log(this.data.oid)
      if (!this.data.oid) {
        return;
      }
      // let list = this.data.info.data
      // list.forEach((item)=>{
      //   item.desc = item.time
      //   item.text = item.context

      // })
      // console.log('list',list)
      // this.setData({
      //   list,
      // })
      getExpress({
        orderId: this.data.oid
      }).then(res => {
        console.log(res)
        const {
          orderStatus
        } = res.data
        let orderInfo = JSON.parse(orderStatus)
        console.log(orderInfo)
        if(orderInfo.result===false){
          wx.showToast({
            title: orderInfo.message,
            icon: 'none'
          })
          return; 
        }
        if (orderInfo.data && Array.isArray(orderInfo.data)) {

          orderInfo.data.forEach((item) => {
            item.desc = item.time
            item.text = item.context

          })
          this.setData({
            list: orderInfo.data
          })
        }
        if(orderInfo.com&&orderInfo.nu){
          console.log(orderInfo.com , expressCompanyList)
         let company=  expressCompanyList.find( item => {
            return item.value  === orderInfo.com
          })

          this.setData({
            companyName : company?company.label:orderInfo.com,
            nu: orderInfo.nu,
          })
        }



      })
    }
  }
})